#!/usr/bin/env python

import os
import urllib
import json

from  datetime import datetime

from google.appengine.api import users
from google.appengine.ext import ndb
from google.appengine.ext import db

import jinja2
import webapp2
import logging

# print os.path.join(os.path.dirname(__file__)

# JINJA_ENVIRONMENT = jinja2.Environment(
#     loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'markup')),
#     extensions=['jinja2.ext.autoescape'],
#     autoescape=True)

####################################################################################################
# Page Handlers

class Index(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        result = {
            'text' : 'hello workasd'
        }
        jsonResult = json.dumps(result)
        self.response.write(jsonResult)

        # self.anonymousRedirect()
        # template = JINJA_ENVIRONMENT.get_template('company/index.html')
        # self.template_values["ACCOUNT_ID"] = self.session["ACCOUNT_ID"]
        # self.response.write(template.render(self.template_values))

        #self.session[self.app_config['SESSION']['ID']] = 'bar'
        #self.response.set_cookie(self.app_config['COOKIES']["AUTHENTICATION"], 'YAHOO', max_age=360, path='/')

        # template = JINJA_ENVIRONMENT.get_template('company_index.html')
        # self.response.set_cookie('page', 'get_company_index', max_age=360, path='/')
        
        # #user_name = self.request.get('user_name', DEFAULT_GUESTBOOK_NAME)

        # # Ancestor Queries, as shown here, are strongly consistent
        # # with the High Replication Datastore. Queries that span
        # # entity groups are eventually consistent. If we omitted the
        # # ancestor from this query there would be a slight chance that
        # # Greeting that had just been written would not show up in a
        # # query.
        # user_query = User.query(
        #     ancestor=user_key()).order(-User.created_date)
        # user_list = user_query.fetch(10)
        # self.template_values["company_list"] = user_list
        
        # self.response.write(template.render(self.template_values))
    def put(self, *args):
        self.response.headers['Content-Type'] = 'application/json'
        result = {
            'text' : 'hello workasd'
        }
        jsonResult = json.dumps(args)
        # logging.error("TEST ERRORLOG")
        # logging.info("TEST INFOLOG")
        # logging.debug("TEST DEBUGLOG")
        # logging.info(args)
        # logging.info(self.request)
        logging.debug("START")
        #logging.info(self.request.body)
        a = json.loads(self.request.body)
        # logging.debug(a)
        # logging.debug("a")
        # logging.debug(a["testInput"])
        # logging.debug("END")
        self.response.write(jsonResult)

####################################################################################################
# Routing

"""ZX: 
    It is recommended that the routing urls have (?i) to make it case-insensitive.
    Routing urls should end with slash (/).
    If there's no slash, browser (eg Google Chrome) might treat it as a document 
    and perform search instead of routing it.
    #], debug=True, config=application_config.webapp2_config)
"""
app = webapp2.WSGIApplication([
    #(r'(?i)^/projects/(\w+)*$', ProjectsPageHandler),
    (r'(?i)^/facility/$', Index)
    # (r'(?i)^/facility/add$', AddFacility),
    # (r'(?i)^/facility/edit/(.+)$', EditFacility),
    # (r'(?i)^/facility/update/(.+)$', UpdateFacility),
    # (r'(?i)^/facility/delete/(.+)$', DeleteFacility)
], debug=True)


